<?php
namespace app\food\controller;

use think\Controller;
use think\Request;
use app\food\model\User;
use think\captcha\Captcha;
use think\Session;

class Auth extends Controller
{
    /**注册显示页面
     * @return 输出模版
     */
    public function register()
    {
        return $this->fetch('register');
    }

    /**保存注册
     * @param Request $request
     */
    public function registStore(Request $request)
    {

        $data = $request->param();

        //如果验证码正确
        if($this->check_verify($data['captcha'])) {
            $User = new User;
            $result = $User->validate(
                [
                    'phone'  => 'require|length:11|number|unique:user',
                    'password'   => 'require|min:6',
                ],
                [
                    'phone.require' => '手机号必须填写',
                    'phone.length' => '手机号格式有误',
                    'password.length' => '密码不能小于6位',
                    'phone.number' => '手机号格式有误',
                ]
            )->save([
                'phone'=> $data['phone'],
                'password' => $data['password'],
            ]);
            if(false === $result){
                // 验证失败 输出错误信息
                Session::flash('registerError',$User->getError());
                Session::flash('registerphone',$data['phone']);
                $this->redirect('/register');
            }
        }else {
            Session::flash('registerError','验证码填写错误');
            Session::flash('registerphone',$data['phone']);
            $this->redirect('/register');
        }
        return $this->redirect('/login');
    }

    /**登录页面
     * @return \think\Response
     */
    public function login() {
        return $this->fetch('login');
    }

    /**登录逻辑
     * @param Request $request
     */
    public function loginStore(Request $request) {
        $data = $request->param();
        //验证码验证
        if($this->check_verify($data['captcha'])) {
            $user = User::get([
                'phone'=> $data['phone'],
                'password'=> $data['password']
            ]);
            if(!$user) {
                Session::flash('loginError','用户名密码不匹配');
                $this->redirect('/login');
            }
            //存session返回到首页
            Session::set('user', $user);
            $this->redirect('/');
        }else {
            Session::flash('loginError','验证码填写错误');
            Session::flash('loginphone',$data['phone']);
            $this->redirect('/login');
        }
    }

    public function logout() {
        //清除user的session
        if(Session::has('user')) {
            Session::delete('user');
            $this->redirect('/');
        }
    }

    public function verify() {
        $config =    [
            //图片宽度
            'imageW'=> 120,
            //图片高度
            'imageH'=> 36,
            // 验证码字体大小
            'fontSize'    =>    15,
            // 验证码位数
            'length'      =>    4,
            // 关闭验证码杂点
            'useNoise'    =>    true,
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }

    private function check_verify($code, $id = ''){
        $captcha = new Captcha();
        return $captcha->check($code, $id);
    }

}
